package com.tianya.common.sercurity;

import java.io.IOException;


/**
* Base64安全编码组件
*
* @author 陈伟
* @version 1.0.0.0
* @since 1.0.0.0
*/
public class Base64Coder {
	/**
     * Base64编码加密
     *
     * @param str
     * @return String
     * @throws IOException
     */
    public static String encodeString(String str) throws IOException
    {
        return encodeString(str.getBytes());
    }
	/**
     * Base64编码加密
     *
     * @param str
     * @return String
     * @throws IOException
     */
    public static String encodeString(byte[] str) throws IOException
    {
        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        String encodedStr = new String(encoder.encodeBuffer(str));
        return encodedStr.trim();
    }

    /**
     * Base64编码解密
     *
     * @param str
     * @return String
     * @throws IOException
     */
    public static String decodeString(String str) throws IOException
    {
        String value = new String(decodeBufferString(str));
        return value;
    }
    public static byte[] decodeBufferString(String str) throws IOException
    {
        sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
        return dec.decodeBuffer(str);
    }
}
